/**
 * 新建系统邮件
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2015-01-20 17:02:50
 * @for webapp.html
 * @version $1.0$
 */
var BSTMG = BSTMG || {};
BSTMG.addsysEmail = {
	init: function(){
		var that = this;
		that.initSelectors();
		that.initEmailParams();
		/* 发送间隔 */
		that.sendInterval();
		/* 确认保存 */
		that.submitEmail();
	},
	initSelectors: function(){
		var that = this;
		that["query"] = {};
		that["query"]["#addNewEmail"] = $("#addNewEmail");
		// 发送间隔
		that["query"]["#sendinterval"] = $("#sendinterval");
		that["query"][".operatorbuttons"] = that["query"]["#addNewEmail"].find(".operatorbuttons");
	},
	initEmailParams: function(){
		var options = {
				width: "100%",
	        	height: "300px",
				cssPath : '/cdn/kindeditor-4.1.10/plugins/code/prettify.css',
				uploadJson : '/cdn/kindeditor-4.1.10/jsp/upload_json.jsp?objectName=product',
				fileManagerJson : '/cdn/kindeditor-4.1.10/jsp/file_manager_json.jsp',
				allowFileManager : false,
				afterCreate : function() {
					var self = this;
					KindEditor.ctrl(document, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
					KindEditor.ctrl(self.edit.doc, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
				}
			};
		var editors = ["email_content","email_signature"];
		KindEditor.ready(function(K) {
			$.each(editors,function(index,val){
				window[val] = K.create( '#' + val , options );
			});
		});
	},
	submitEmail: function(){
		var that = this,
			$operators = that["query"][".operatorbuttons"],
			$addNewEmail = that["query"]["#addNewEmail"],
			$input = $addNewEmail.find("input"),
			$emailSubject = $input.filter("input[name=emailSubject]"),
			$emailType = $addNewEmail.find("select[name=emailType]"),
			$fromAddress = $input.filter("input[name=fromAddress]"),
			$emailContent = $('#email_content'),
			$emailSign = $('#email_signature'),
			$isInterval = that["query"]["#sendinterval"].find("input.simple"),
			$timeSpace = that["query"]["#sendinterval"].find("select[name=timeSpace]");
			
		$operators.on("click",".submit",function(){
//			var data = getData();
//			if( !!!data ){
//				return false;
//			};
			window["email_content"].sync();
			window["email_signature"].sync();
			var datas = $('.form-horizontal').serialize();
			//保存邮件类型信息
			$.ajax({
				type: 'POST',
				url: "/sysAdmin/operate/email/addSysEmail",
				data: datas,
				dataType: 'json',
				success: function(data){
					if(data.isSuccess) {
						location.href="/sysAdmin/email/sysEmailManager";
					}else {
						BSTYUN.dialog.warn("邮件类型信息保存失败，请重新保存！");
					}
				},
				error: function(){
					BSTYUN.dialog.warn("网络异常，请检查网络！");
				}
			});
		});
		function getData(){
			window["email_content"].sync();
			window["email_signature"].sync();
			var emailSubject = $.trim( $emailSubject.val() ),
				emailType = $.trim( $emailType.val() ),
				fromAddress = $.trim( $fromAddress.val() ),
				emailContent = $emailContent.val(),
				emailSign = $emailSign.val();
			var timeSpace = $isInterval.prop("checked") ? $.trim( $timeSpace.val() ) : 0 ;
			if( emailSubject == "" ){
				BSTYUN.dialog.warn("主题不能为空！")
				return false;
			};
			if( emailType == "" ){
				BSTYUN.dialog.warn("类型不能为空！")
				return false;
			};
			if( fromAddress == "" ){
				BSTYUN.dialog.warn("发件人不能为空！")
				return false;
			};
			if( emailContent == "" ){
				BSTYUN.dialog.warn("内容不能为空！")
				return false;
			};
			if( emailSign == "" ){
				BSTYUN.dialog.warn("签名不能为空！")
				return false;
			};
			return {
				emailSubject: emailSubject,
				emailType: emailType,
				fromAddress: fromAddress,
				emailContent: emailContent,
				emailSign: emailSign,
				timeSpace: timeSpace
			};
		};
	},
	sendInterval: function(){
		var that = this,
			$sendinterval = that["query"]["#sendinterval"],
			$input = $sendinterval.find("input"),
			$select = $sendinterval.find("select");
		$input.on("click",function(){
			var $this = $(this),
				ischecked = $this.prop("checked");
			if( ischecked ){
				$this.parent().next("fieldset").prop("disabled",false);
			}else{
				$this.parent().next("fieldset").prop("disabled",true);
			};
		});
	}
};

$(function(){
	BSTMG.addsysEmail.init();
});
